package lc;

import org.junit.Test;

public class Ex7 {
    class Solution {
        public int reverse(int x) {
            boolean flag = x >= 0;
            if (x == Integer.MIN_VALUE) return 0;
            if (!flag) x = -x;
            int res = 0, t, limit = Integer.MAX_VALUE / 10;
            while (x != 0) {
                t = x % 10;
                x /= 10;
                if (res > limit || res == limit &&
                ((flag && t >= 7)|| (!flag && t >= 8))) {
                    return 0;
                }
                res = res * 10 + t;
            }

            return flag ? res : -res;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.reverse(-2147483648));        
    }
}
